//
// Created by 罗外林 on 2024/1/3.
// https://github.com/ChinaZeng/NativeEglDemo/blob/master/app/src/main/cpp/egl/EglThread.h

#ifndef MY_APPLICATION_EGLTHREAD_H
#define MY_APPLICATION_EGLTHREAD_H

#include "pthread.h"
#include "android/native_window.h"
#include "EglHelper.h"
#include "EGL/eglplatform.h"

#define RENDER_MODULE_AUTO 1
#define RENDER_MODULE_MANUAL 2

typedef void(*OnCreate)();
typedef void(*OnChange)(int width, int height);
typedef void (*OnDraw)();

class EglThread {
private:
    enum STATE{
        NO_SURFACE, // 没有有效的Surface
        FRESH_SURFACE, // 持有一个为初始化的新的Surface
        RENDERING, // 初始化完毕，可以开始渲染
        SURFACE_DESTROY, // surface销毁
        STOP // 停止绘制
    };
private:
    pthread_t mEglThread = -1;
    ANavtiveWindow *_nativeWindow = null_ptr;
    int _renderType = RENDER_MODULE_MANUAL;
    STATE _state = NO_SURFACE;
    int _surfaceWidth = 0;
    int _surfaceHeight = 0;

    OnCreate _onCreate;
    OnChange  _onChange;
    OnDraw _onDraw;

    pthread_mutex_t _pthreadMutex;
    pthread_cond_t _pthreadCond;

    bool  _isExit;

    static void *eglThreadImpl(void *context);
public:
    EglThread();
    ~EglThread();

    //生命周期
    void onSurfaceCreate(EGLNativeWindowType windowType);
    void onSurfaceChange(int width, int height);

    // 设置模式
    void setRenderModule(int renderModule);
    void notifyRender();

    // 设置回调
    void callbackOnCreate(OnCreate oncreate);
    void callbackOnChange(OnChange onChange);
    void callbackOnDraw(OnDraw onDraw);
};


#endif //MY_APPLICATION_EGLTHREAD_H
